package omq.api;

//import java.net.MalformedURLException;
//import java.net.URL;
//import java.rmi.RemoteException;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Calendar;

import javax.xml.rpc.ServiceException;
/**
 * 
 * @author hagaii
 *
 */
public abstract class OMQConnection { // implements OmqConnectionInterface {
	
	// user, password and connection objects
	// these are 
	protected String username;
	protected String password;
	protected String connectionURL;
	private OMQConnectionType connectionType;
	// omq API objects
	protected OMQQueueManager qmgr;
	protected OMQQueue queue;
	
	// IMPORTNAT !!!! from http://onlinemq.com/support/index.php/Online_MQ_API:
	// because SOAP already has a specific enveloping for each action, the action field in the 
	// canonical is ignored for the requests. It is returned in the response, but can be ignored.
	// so when working soap we don't really need to work with this action String
	protected String action;

	/**
	 * Create a new connection to OnlineMQ Server. The Connection URL and Type must be specified in this constructor.
	 * @param username
	 * @param password
	 * @param connectionURL
	 * @param connectionType
	 * @param qmgr
	 * @throws ServiceException
	 */
	public OMQConnection(String username, String password, String connectionURL, OMQConnectionType connectionType, OMQQueueManager qmgr) throws ServiceException 
	{
		this.username = username;
		this.password = password;
		this.connectionURL = connectionURL;
		this.connectionType = connectionType;
		this.qmgr = qmgr;

		//TODO should we save the URL as URL object? and also check it?
	
//		try {
//			setUrl(new URL(locator.getOnlineMQWebserviceSoapPortAddress()));
//	    } catch (MalformedURLException e) {
//	        e.printStackTrace();
//	    }
	}
	
	// trying to imitate JMS behaviour
	/**
	 * Create a new session within this specific connection.
	 */
	public OMQSession createSession(boolean transacted)
	{
        OMQSession session = new OMQSession(this, transacted);

        return session;
	}
	
	// OMQ server API methods
	
	/**
	 * Send a message to a queue.
	 * @param queue
	 * @param msg
	 * @return
	 * @throws RemoteException
	 */
	protected abstract int sendMessage(OMQQueue queue, OMQMessage msg) throws RemoteException;
	/**
	 * Receive a message from a queue.
	 * @param queue
	 * @return
	 * @throws RemoteException
	 * @throws IOException
	 */
	protected abstract OMQMessage receiveMessage(OMQQueue queue) throws RemoteException ,IOException ;
	/**
	 * Peek a message on a queue.
	 * @param queue
	 * @return
	 * @throws RemoteException
	 * @throws IOException
	 */
	protected abstract OMQMessage peekMessage(OMQQueue queue) throws RemoteException ,IOException ;
	/**
	 * Check connection to OMQ server, validate user-password and their permission to use a Queue-Manager and Queue.
	 * All parameters are mandatory.
	 * @param queue
	 * @return
	 * @throws RemoteException
	 */
	public abstract int checkConnection(OMQQueue queue) throws RemoteException;
	/**
	 * Clean all messages from a queue.
	 * @param queue
	 * @return
	 * @throws RemoteException
	 */
	protected abstract int emptyQueue(OMQQueue queue) throws RemoteException;
	/**
	 * Retrieve Queue definitions.
	 * @param queue
	 * @return
	 * @throws RemoteException
	 */
	protected abstract OMQQueue getQueueDefinitions(OMQQueue queue) throws RemoteException;

	// additional OMQ client API methods
	protected abstract String getLastMessageID();
	public abstract int getLastErrorCode();
	public abstract String getLastErrorDescription();
}
